
var app = getApp();

Page({
  data : {
    addressList : {}
  },
  addAddress : function(e){
    var address_id = e.currentTarget.dataset.id;
    console.log("address_id:" + address_id);
    wx.redirectTo({
      url: '/pages/address/add/add?address_id=' + address_id
    })
  },
  onLoad : function(options){
    this.uid = wx.getStorageSync('uid');
  },
  onShow : function(){
    this.loadAddressList();
  },
  loadAddressList : function(){
    var access_token = wx.getStorageSync('access_token');
    var page = this;
    /** 
    wx.request({
      //url: 'https://shizhencaiyuan.com/groupAdmin.php/Home/Address/getAddress',
      url:'http://core-dev.dzxwapp.com/parse/classes/ShipAddress',
      //data: {data:JSON.stringify({uid: this.uid,openid : null})},
      dara: { },
      method: 'get',
      header: {
        'X-Parse-Application-Id': 'jiKapGuqdTzWg62WgkDMKNUo',
        'X-Parse-Session-Token': access_token,
        'content-type': 'application/json'
      },
      success: function(res){
        console.log("addressList:" + JSON.stringify(res));
       // console.log("addressList:" + JSON.stringify(res.data.data.results));
        page.setData({ addressList: res.data.results})
        wx.setStorageSync('addressList', res.data.data.results)
      }
    })
    */
    app.request.wxRequestForDZXW({
      url: 'classes/ShipAddress',
      method: 'GET',
      success: function (res) {
          page.setData({ addressList: res.data.results })
          wx.setStorageSync('addressList', res.data.results)
          var address = res.data.results
          wx.setStorageSync('addressList', res.data.results)
          for (var i = 0; i < address.length; i++) {
            var dft = address[i].dft;
            if (dft == 'true' || dft == true) {
              wx.setStorageSync('address', address[i])
              var address1 = wx.getStorageSync('address')
              console.log("---------存储到缓存中address:" + JSON.stringify(address1) + ",detail:" + dft);
              break;
            }
          }
      }
    })
  },
  setDefaultAddress : function(e){
    var access_token = wx.getStorageSync('access_token');
    var detail = e.detail.value;
    var page = this;
    var url = 'http://core-dev.dzxwapp.com/parse/classes/ShipAddress/' + detail;
    var data = { 'dft': true };
    var method = 'PUT';
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'X-Parse-Application-Id': 'jiKapGuqdTzWg62WgkDMKNUo',
        'X-Parse-Session-Token': access_token,
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log("createShipAddress:" + JSON.stringify(res));
        wx.redirectTo({
          url: '/pages/address/list/list'
        })
      },
      fail: function (res) {
        // fail
      }
    })
    /** 
    wx.request({
      url: 'https://shizhencaiyuan.com/groupAdmin.php/Home/Address/saveDefault',
      data: {data:JSON.stringify({uid: this.uid,openid : null,address_id : detail[0]})},
      method: 'POST',
      header: {
          'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res){
         wx.redirectTo({
           url: '/pages/address/list/list'
         })
      },
      fail: function(res) {
        // fail
      }
    })
     */

  },
  delAddress : function(e){
    var address_id = e.currentTarget.dataset.id;
    var page = this;
    var access_token = wx.getStorageSync('access_token');
    wx.showModal({
      title: '确定删除地址吗！',
      success: function(res) {
        if (res.confirm) {
          var url = 'http://core-dev.dzxwapp.com/parse/classes/ShipAddress/' + address_id;
          wx.request({
            url: url,
            method: 'DELETE',
            header: {
              'X-Parse-Application-Id': 'jiKapGuqdTzWg62WgkDMKNUo',
              'X-Parse-Session-Token': access_token,
              'content-type': 'application/json'
            },
            success: function(res){
              wx.redirectTo({
                url: '/pages/address/list/list'
              })
            }
          })
        } 
      }
    })
  },
  backHome : function(e){
    app.backHome();
  }
})